<template>
  <div class="app-container">
    医院设置列表
    <br><br>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="医院名称">
        <el-input v-model="params.hosname" placeholder="医院名称"></el-input>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="params.hoscode" placeholder="医院编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getPage(1)">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" icon="el-icon-delete" @click="removeByIds">批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="list"
      stripe
      style="width: 100%"
      ref="table"
    >
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column type="index" label="日期" width="50"/>
      <el-table-column prop="hosname" label="医院名称"/>
      <el-table-column prop="hoscode" label="医院编号"/>
      <el-table-column prop="apiUrl" label="API基础路径" width="200"/>
      <el-table-column prop="contactsName" label="联系人姓名"/>
      <el-table-column prop="contactsPhone" label="联系人手机"/>
      <el-table-column label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? '可用' : '不可用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button type="primary"
                     @click="lock(scope.row.id,0)" v-if="scope.row.status === 1" @>锁定
          </el-button>
          <el-button type="primary"
                     @click="lock(scope.row.id,1)" v-if="scope.row.status !== 1">解锁
          </el-button>
          &nbsp;&nbsp;
          <router-link :to="'/hospitalSet/edit/'+scope.row.id">
            <el-button type="primary">修改
            </el-button>
          </router-link>
          &nbsp;&nbsp;
          <el-popconfirm title="确定删除吗？" @onConfirm="removeDataById(scope.row.id)">
            <el-button slot="reference" type="danger" icon="el-icon-delete" circle></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <el-pagination background
                   style="text-align:center;"
                   :current-page="current"
                   :page-size="limit"
                   :total="total"
                   :page-sizes="[1,20, 50, 100, 500]"
                   layout="total, sizes, prev, pager, next, jumper"
                   @current-change="getPage"
                   @size-change="sizeChange">
    </el-pagination>
  </div>
</template>

<script>
import {findPage, removeById, removeByIds, lock} from '@/api/hospitalSet'

export default {
  data() {
    return {
      current: 1,
      limit: 20,
      total: 0,
      params: {},
      list: []
    }
  },
  created() {
    this.getPage()
  },
  methods: {
    sizeChange(limit) {
      this.limit = limit
      this.getPage()
    },
    getPage(page = this.current) {
      this.current = page
      findPage(this.current, this.limit, this.params)
        .then(res => {
          const {data} = res
          this.list = data.records
          this.current = data.current
          this.limit = data.size
          this.total = data.total
        })
        .catch(err => {
          this.$message.error(err);
        })
    },
    removeDataById(id) {
      removeById(id).then(res => {
          if (res.code === 200) {
            this.getPage()
            this.$message({
              message: '删除成功！',
              type: 'success'
            });
          } else {
            this.$message.error(res.message);
          }
        }
      ).catch(err => {
        this.$message.error(err);
      })
    },
    removeByIds() {
      const selection = this.$refs.table.selection;
      const ids = [];
      if (selection.length > 0) {
        this.$confirm('此操作将永久删除选中的医院信息, 是否继续?', '删除确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          for (let i = 0; i < selection.length; i++) {
            ids.push(selection[i].id)
          }
          removeByIds(ids).then(res => {
            this.getPage()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      } else {
        this.$message({
          type: 'info',
          message: '请选择要删除的医院设置'
        });
      }
    },
    lock(id, status) {
      lock(id, status).then(res => {
        this.getPage()
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
      })
    }
  }
}
</script>
